package com.exam.system.controller;

import com.exam.system.entity.ExamQuestion;
import com.exam.system.entity.PageDTO;
import com.exam.system.entity.ResponseEntity;
import com.exam.system.redis.RedisUtils;
import com.exam.system.service.ExamLibraryService;
import com.exam.system.service.ExamQuestionService;
import com.exam.system.service.ExamTagsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags = "题目模块")
@RestController
@CrossOrigin
@RequestMapping("/question/")
public class ExamQuestionController {
    @Autowired
    private ExamQuestionService examQuestionService;
    @Autowired
    private ExamTagsService examTagsService;
    @Autowired
    private RedisUtils redisUtils;

    @ApiImplicitParam(value="分页查找所有的试题以及对应的试题标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="当前页面",required = true),
            @ApiImplicitParam(name="pageSize",value="每页条数",required = true)
    })
    @GetMapping("findAll")
    public ResponseEntity<List<Object>> findAll(Integer page, Integer pageSize) {
        List<Object> list = examQuestionService.findAllQuestionAndTags(page, pageSize);
        ResponseEntity<List<Object>> responseEntity=new ResponseEntity<>(200,"success",list);
        return responseEntity;
    }


    @ApiOperation(value="根据查询条件和分页参数进行多条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="当前页码",required = true),
            @ApiImplicitParam(name="pageSize",value="每页条数",required = true),
            @ApiImplicitParam(name="question",value="查询的题干",required = true),
            @ApiImplicitParam(name="tagId",value="查询的标签Id",required = true),
            @ApiImplicitParam(name="type",value="查询的题目类型,单选，多选等等",required = true),
    })
    @GetMapping("search")
    public ResponseEntity<List<Object>> search(Integer page, Integer pageSize, String question, String  tags, String type) {
        List<Object> list = examQuestionService.findQuestionsByQueryAndTags(page, pageSize, question, tags, type);
        ResponseEntity<List<Object>> responseEntity=new ResponseEntity<>(200,"success",list);
        return responseEntity;
    }


    @ApiOperation(value="根据试题id禁用,修改status状态为n")
    @ApiImplicitParam(name="id",value="试题id",required = true)
    @GetMapping("forbid")
    public ResponseEntity<Object> forbid(Integer id) {
        examQuestionService.forbidden(id);
        ResponseEntity<Object>responseEntity=new ResponseEntity<>(200,"禁用成功",null);
        return responseEntity;
    }

    @ApiOperation(value="根据试题id启用,修改status状态为y")
    @ApiImplicitParam(name="id",value="试题id",required = true)
    @GetMapping("enable")
    public ResponseEntity<Object> enable(Integer id) {
        examQuestionService.enable(id);
        ResponseEntity<Object>responseEntity=new ResponseEntity<>(200,"启用成功",null);
        return responseEntity;
    }

    @ApiOperation(value="根据试题id查找试题")
    @ApiImplicitParam(name="id",value="试题id",required = true)
    @GetMapping("findByQuesId")
    public ResponseEntity<ExamQuestion> findByQuesId(Integer id){
        ExamQuestion question = examQuestionService.findByQuesId(id);
        ResponseEntity<ExamQuestion> responseEntity=new ResponseEntity<>(200,"success",question);
        return  responseEntity;
    }

    /**
     * 根据题目类型和题库查询题目
     * @param questionPageDTO 分页工具类
     * @param quesBankName 题库名称
     * @return 返回对应的题目
     */
    @ApiImplicitParam(name = "questionPageDTO", value = "分页工具类", required = false)
    @ApiOperation(value = "添加试卷时选择题目时的多条件分页查询")
    @PostMapping("findQuestionByTypeAndLibrary")
    public ResponseEntity<PageInfo<ExamQuestion>> findQuestionByTypeAndLibrary(@RequestBody PageDTO<ExamQuestion> questionPageDTO, String quesBankName) {
        return new ResponseEntity<>(200, "获取题目成功", examQuestionService.findQuestionByTypeAndLibrary(questionPageDTO.getCurrentPage(), questionPageDTO.getPageSize(), questionPageDTO.getEnt().getType(), quesBankName));
    }

    /**
     * 根据题目类型和题库查询题目和答案
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "questionPageDTO", value = "分页工具类", required = false),
            @ApiImplicitParam(name = "quesBankName", value = "题目名称", required = false)
    })
    @ApiOperation(value = "添加试卷时选择题目时的多条件分页查询,返回所以的题目和对应的答案")
    @PostMapping("findQuestionAndAnswerByTypeAndLibrary")
    public ResponseEntity<PageInfo<ExamQuestion>> findQuestionAndAnswerByTypeAndLibrary(@RequestBody PageDTO<ExamQuestion> questionPageDTO, String quesBankName) {
        return new ResponseEntity<>(200, "获取题目成功", examQuestionService.findQuestionAndAnswerByTypeAndLibrary(questionPageDTO.getCurrentPage(), questionPageDTO.getPageSize(), questionPageDTO.getEnt().getType(), quesBankName));
    }

    /**
     * 根据试卷id查询所有题目
     * @return 返回试卷上的所有题目
     */
    @ApiImplicitParam(name = "id",value = "试卷id",required = true)
    @ApiOperation(value = "根据试卷id查询所有的题目")
    @GetMapping("findQuestionByPaperId")
    public ResponseEntity<List<ExamQuestion>> findQuestionByPaperId(Integer id){
        return new ResponseEntity<>(200, "查询成功", examQuestionService.findQuestionByPaperId(id));
    }

    @ApiOperation(value = "批量启用试题")
    @ApiImplicitParam(name="list",value="题目集合",required = true)
    @PostMapping("enableBat")
   public ResponseEntity<Object> enableBat(@RequestBody() List<ExamQuestion> list){
        examQuestionService.enableBat(list);
        return new ResponseEntity<>(200,"success",null);
    }

    @ApiOperation(value = "批量禁用试题")
    @ApiImplicitParam(name="list",value="题目集合",required = true)
    @PostMapping("forbiddenBat")
    public ResponseEntity<Object> forbiddenBat(@RequestBody() List<ExamQuestion> list){
        examQuestionService.forbiddenBat(list);
        return new ResponseEntity<>(200,"success",null);
    }

    @ApiOperation(value="根据试题id删除试题")
    @ApiImplicitParam(name="id",value="题目id",required = true)
    @GetMapping("delQuestion")
   public ResponseEntity<Object> delQuestion(Integer id){
        examQuestionService.delQuestion(id);
       return new ResponseEntity<>(200,"success",null);
   }
}
